package cn.hrlly.tao2shou.presenter;

import android.content.Context;

import cn.hrlly.tao2shou.biz.RegisterBiz;
import cn.hrlly.tao2shou.biz.RegisterBizImpl;
import cn.hrlly.tao2shou.domain.User;
import cn.hrlly.tao2shou.listener.OnRegisterListener;
import cn.hrlly.tao2shou.view.view_interface.CollectUserInfoView;
import cn.hrlly.tao2shou.view.view_interface.RegisterView;

/**
 * 注册逻辑实现
 * User: gengliang
 * Date: 2016-01-26
 * Time: 19:32
 */

public class RegisterPresenterImpl implements OnRegisterListener,RegisterPresenter {

    private RegisterBiz registerBiz;
    private RegisterView registerView;
    private CollectUserInfoView collectUserInfoView;

    public RegisterPresenterImpl(RegisterView registerView,Context context){
        this.registerBiz=new RegisterBizImpl(this,context);
        this.registerView=registerView;
    }

    public RegisterPresenterImpl(CollectUserInfoView collectUserInfoView,Context context){
        this.registerBiz=new RegisterBizImpl(this,context);
        this.collectUserInfoView=collectUserInfoView;
    }

    //以下是registerPresenter接口的实现，用于联网
    @Override
    public void getCode() {
        User user=new User();
        user.setPhoneNumber(registerView.getPhoneNumber());
        registerBiz.getVerificationCode(user);
    }

    @Override
    public void judgeCode() {
        User user=new User();
        user.setPhoneNumber(registerView.getPhoneNumber());
        user.setVerificationCode(registerView.getVerificationCode());
        registerBiz.judgeVerificationCode(user);
    }

    @Override
    public void collectUserInfo() {
        User user=new User();
        user.setNickname(collectUserInfoView.getNickname());
        user.setGender(collectUserInfoView.getGender());
        user.setSchool(collectUserInfoView.getSchool());
        user.setPhoneNumber(collectUserInfoView.getPhoneNumber());
        user.setPassword(collectUserInfoView.getPassword());
        user.setLongditude(collectUserInfoView.getLongitude());
        user.setLantitude(collectUserInfoView.getLantitude());
        registerBiz.collectUserInfo(user,collectUserInfoView.getPortraitFile());
    }

    @Override
    public void closeDataBase() {
        registerBiz.closeDataBase();
    }

    //以下是onRegisterListener接口实现
    @Override
    public void getVerificationCodeFail(String errorMessage) {
        registerView.showMessage(errorMessage);
    }

    @Override
    public void getVerificationCodeFail(int errorMessage) {
        registerView.showMessage(errorMessage);
    }

    @Override
    public void judgeVericationCodeSucc() {
        registerView.startActivity();
    }

    @Override
    public void judgeVericationCodeFail(int errorMessage) {
        registerView.showMessage(errorMessage);
    }

    @Override
    public void judgeVericationCodeFail(String errorMessage) {
        registerView.showMessage(errorMessage);
    }

    @Override
    public void collectUserInfoSucc(final User user) {
        collectUserInfoView.startActivity();

        //将用户信息存在数据库中，在io线程操作
        registerBiz.saveUserInfo(user);
    }

    @Override
    public void collectUserInfoFail(int errorMessage) {
        collectUserInfoView.showMessage(errorMessage);
    }

    @Override
    public void collectUserInfoFail(String errorMessage) {
        collectUserInfoView.showMessage(errorMessage);
    }
}
